package com.example.blog.controller;

import com.example.blog.constant.Constants;
import com.example.blog.model.BlogInfo;
import com.example.blog.model.Result;
import com.example.blog.service.BlogService;
import com.example.blog.utils.JwtUtils;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/blog")
@Slf4j
public class BlogController {

    @Autowired
    private BlogService blogService;

    @RequestMapping("getList")
    public List<BlogInfo> getList() {
        log.info("获取博客列表...");
        return blogService.getList();
    }

    @RequestMapping("/getBlogDetail")
    public BlogInfo getBlogDetail(Integer blogId, HttpServletRequest request) {
        log.info("根据博客id获取博客详情:" + blogId);
        BlogInfo blogInfo = blogService.getBlogDetail(blogId);

        //获取登录用户
        //获取token
        String token = request.getHeader(Constants.REQUEST_HEADER_TOKEN);
        //从token中获取登录用户id
        Integer userId = JwtUtils.getIdByToken(token);

        if (userId != null && blogInfo.getUserId() == userId) {
            blogInfo.setIsLoginUser(true);
        } else {
            blogInfo.setIsLoginUser(false);
        }
        return blogInfo;
    }

    @RequestMapping("/add")
    public Boolean addBlog(@RequestBody BlogInfo blogInfo, HttpServletRequest request) {
        log.info("添加博客,blogInfo:{}", blogInfo);
        //参数校验
        if (!StringUtils.hasLength(blogInfo.getTitle()) || !StringUtils.hasLength(blogInfo.getContent())) {
            return false;
        }
        //获取登录用户
        //获取token
        String token = request.getHeader(Constants.REQUEST_HEADER_TOKEN);
        //从token中获取登录用户id
        Integer userId = JwtUtils.getIdByToken(token);
        if(userId == null) {
            return false;
        }
        blogInfo.setUserId(userId);

        blogService.insertBlog(blogInfo);
        return true;
    }

    @RequestMapping("/update")
    public Boolean update(BlogInfo blogInfo) {
        log.info("更新博客,blogId:" + blogInfo.getId());
        //参数校验
        if(blogInfo.getId() == null ||
                !StringUtils.hasLength(blogInfo.getTitle()) ||
                !StringUtils.hasLength(blogInfo.getContent())) {
            return false;
        }
        Integer result =  blogService.update(blogInfo);
        return true;
    }

    @RequestMapping("/delete")
    public Boolean delete(Integer blogId) {
        log.info("删除博客: blogId=" + blogId);
        if(blogId == 0 || blogId <= 0) {
            return false;
        }
        BlogInfo blogInfo = new BlogInfo();
        blogInfo.setId(blogId);
        blogInfo.setDeleteFlag(1);
        blogService.update(blogInfo);
        return true;
    }

    @RequestMapping("/getNumberOfBlogs")
    public Integer getNumberOfBlogs(Integer userId) {
        log.info("获取博客数量: userId: " + userId);
        Integer count = blogService.getNumberOfBlogs(userId);
        return count;
    }
}
